<?php
/**
 * Created by PhpStorm.
 * User: iamya
 * Date: 2017/6/23
 * Time: 9:31
 */

namespace app\system\controller;


use app\common\model\SeoModel;
use think\Exception;
use think\Request;

class SeoController extends BaseController
{
    private $seoModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->seoModel = new SeoModel();
    }

    public function index()
    {
        $list = $this->seoModel->paginate();
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->post();
            $result = $this->validate($data, 'SeoValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->seoModel->addData($data)) {
                $this->success('添加成功', 'index');
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->post();
            $result = $this->validate($data, 'SeoValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->seoModel->editData($data)) {
                $this->success('修改成功', 'index');
            } else {
                $this->error('修改失败');
            }
        }
        $id   = $this->request->param('id', 0);
        $info = $this->seoModel->find($id);
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function del()
    {
        $id = $this->request->param('id', 0);
        if ($this->seoModel->delData($id)) {
            $this->success('删除成功', 'index');
        } else {
            $this->error('删除失败');
        }

    }

    public function robots()
    {

        $robotsFile = ROOT_PATH . '/robots.txt';
        $botList    = [
            'baidu'  => [
                'name'  => '百度',
                'value' => ['Baiduspider', 'Baiduspider-image'],
            ],
            '360'    => [
                'name'  => '360',
                'value' => ['360Spider'],
            ],
            'google' => [
                'name'  => 'google',
                'value' => ['Googlebot'],
            ],
            'msn'    => [
                'name'  => 'msn',
                'value' => ['MSNBot'],
            ],
            'youdao' => [
                'name'  => '有道',
                'value' => ['YoudaoBot'],
            ],
            'sogou'  => [
                'name'  => '搜狗',
                'value' => ['Sogou web spider', 'Sogou inst spider', 'Sogou spider2', 'Sogou blog', 'Sogou News Spider', 'Sogou Orion spider']],
            'soso'   => [
                'name'  => '搜搜',
                'value' => ['Sosospider'],
            ],
        ];
        $sysDir     = [
            '系统管理'   => '/app/system/',
            '店铺管理'   => '/app/site/',
            '插件模块'   => '/app/plus/',
            '配置模块'   => '/app/extra/',
            '公共函数模块' => '/app/common/',
            '前端展示模块' => '/app/home/',
        ];
        $default    = ['User-agent' => ['*'],
                       'Disallow'   => ['/app/site/', '/app/system/', '/app/plus/', '/app/extra/', '/app/common/'],
                       'Sitemap'    => [url('plus/Seo/sitemap', '', '', true)],
        ];
        if ($this->request->isPost()) {
            $str               = ['User-agent' => ['*'], 'Disallow' => [], 'Sitemap' => ''];
            $user_agent_status = $this->request->post('User-agent-status', 0);
            if ($user_agent_status == 1) {
                $key = $this->request->post('User-agent/a', []);
                foreach ($key as $value) {
                    $str['User-agent'] = array_merge($str['User-agent'], $botList[$value]['value']);
                }
            }
            $str['User-agent'] = array_reverse($str['User-agent']);
            $str['Disallow']   = $this->request->post('Disallow/a', []);
            $str['Sitemap']    = [$this->request->post('Sitemap', $default['Sitemap'])];
            $robots            = '';
            foreach ($str as $item => $value) {
                foreach ($value as $i => $v) {
                    $robots .= $item . ':' . $v . PHP_EOL;
                }
            }
            try {
                file_put_contents($robotsFile, $robots);
            } catch (Exception $e) {
                $this->error($e);
            }
            $this->success('生成成功');
        }
        $robot = [];
        if (file_exists($robotsFile)) {
            $content = file_get_contents($robotsFile);
            $list    = explode(PHP_EOL, $content);
            foreach ($list as $value) {
                $value          = str_replace('http://', 'http', $value);
                $v              = explode(':', $value);
                $v[1]           = str_replace('http', 'http://', $v[1]);
                $robot[$v[0]][] = $v[1];
            }
            $disallow = [];
            foreach ($robot['User-agent'] as $value) {
                foreach ($botList as $k => $v) {
                    if (in_array($value, $v['value'])) {
                        $disallow[] = $k;
                        break;
                    }
                }
            }
            $robot['User-agent'] = $disallow;
            $this->assign('robots', $robot);
        } else {
            $this->assign('robots', $default);
        }
        $this->assign('sysDir', $sysDir);
        $this->assign('botList', $botList);
        return $this->fetch();
    }
}